import java.util.*;


public class Solution2 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 返回表达式的值
     * @param s string字符串 待计算的表达式
     * @return int整型
     */
    public int solve (String s) {
        // write code here
        Stack<Integer> s1 = new Stack<>();//装数字
        Stack<Character> s2 = new Stack<>();//装符号
        //让每个括号的内容递归
        char[] ch = s.toCharArray();
        for(int i = 0;i<s.length();i++) {
            if(!isoprate(ch[i])) {
                ArrayList<Integer> list = number(i,ch);
                s1.push(list.get(0));
                i = list.get(1)-1;
            } else if(ch[i] == '(') {
                int j = i;
                int count = 1;//左括号+1
                while(count != 0) {
                    j++;
                    if(ch[j] == '(') {
                        count ++;
                    }
                    if(ch[j] == ')') {
                        count--;
                    }
                }
                int ret = solve(s.substring(i+1,j));
                s1.push(ret);
                i = j;
            } else if(ch[i] == '+' || ch[i] == '-') {
                s2.push(ch[i]);
            } else {
                //遇到乘号要计算
                //下一个是数
                if(!isoprate(ch[i+1])) {
                    ArrayList<Integer> list = number(i+1,ch);
                    i = list.get(1)-1;
                    int num = s1.pop();
                    int num1 = list.get(0);
                    int ret = num * num1;
                    s1.push(ret);

                } else {
                    //不是数时，是括号
                    i++;
                    int j = i;
                    int count = 1;//左括号+1
                    while(count != 0) {
                        j++;
                        if(ch[j] == '(') {
                            count ++;
                        }
                        if(ch[j] == ')') {
                            count--;
                        }
                    }

                    int ret = solve(s.substring(i+1,j));
                    i = j;
                    int num = s1.pop();
                    ret *= num;
                    s1.push(ret);
                }
            }

        }
        while(!s2.isEmpty()) {
            int i = s1.pop();
            int j = s1.pop();
            char ch1 = s2.pop();
            if(ch1 == '+') {
                j += i;
            } else {
                if(!s2.isEmpty() && s2.peek() == '-') {
                    j += i;
                } else {
                    j -= i;
                }

            }
            s1.push(j);
        }
        return s1.pop();
    }
    //判断是不是符号
    private boolean isoprate(char ch) {
        if(ch == '(' || ch == ')' || ch == '+' || ch == '-' || ch == '*') {
            return true;
        }
        return false;
    }

    //如果是数字，则要取出完整数字
    private ArrayList<Integer> number(int i,char[] ch) {
        ArrayList<Integer> list = new ArrayList<>();
        StringBuilder s = new StringBuilder();
        while(i<ch.length && !isoprate(ch[i])) {
            s.append(ch[i++]);
        }
        list.add(Integer.parseInt(s.toString()));
        list.add(i);
        return list ;
    }


}